<template>
  <div class="page_dict_data app-container" v-loading="loading">
    <cfCard title='' class='page-container-search bottom12'>
      <div slot='content'>
        <el-form :model="searchParams" ref="searchParams" size="small" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="字典名称" prop="dictType">
            <el-select v-model="searchParams.dictType" clearable @change="handleSearch">
              <el-option v-for="item in typeOptions" :key="item.dictId" :label="item.dictName" :value="item.dictType"/>
            </el-select>
          </el-form-item>
          <el-form-item label="字典标签" prop="dictLabel">
            <el-input v-model="searchParams.dictLabel" placeholder="请输入字典标签" clearable v-search="handleSearch"/>
          </el-form-item>
          <el-form-item label="状 态" prop="status">
            <el-select v-model="searchParams.status" placeholder="数据状态" clearable @change="handleSearch">
              <el-option v-for="dict in sysNormalDisable" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue"/>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleSearch">查询</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="handleReset">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </cfCard>
    <cfCard title='字典数据' hasTable :tableObj='tableDataObj' class='page_house-container-bottom'>
      <!--表格区域-->
      <template slot='btn'>
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['system:dict:add']">新增</el-button>
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['system:dict:export']" >导出</el-button>
      </template>
      <template slot='content'>
        <cfTable ref="cfTable" :tableDataObj='tableDataObj' :tableData='tableData'>
          <template slot="dictLabel">
            <el-table-column label='字典标签' min-width='100px'>
              <template slot-scope="scope">
                <span v-if="scope.row.listClass == '' || scope.row.listClass == 'default'">{{scope.row.dictLabel}}</span>
                <el-tag v-else :type="scope.row.listClass == 'primary' ? '' : scope.row.listClass">{{scope.row.dictLabel}}</el-tag>
              </template>
            </el-table-column>
          </template>
          <template slot="status">
            <el-table-column label='状 态' width='100px' align='center'>
              <dict-tag slot-scope="scope" :options="sysNormalDisable" :value="scope.row.status"/>
            </el-table-column>
          </template>
          <template slot="createTime">
            <el-table-column label='创建时间' width='180px'>
              <span slot-scope="scope">{{ parseTime(scope.row.createTime) }}</span>
            </el-table-column>
          </template>
          <template slot='operate'>
            <el-table-column label='操 作' width='120px' align='right'>
              <span slot-scope='scope'>
                <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:dict:edit']">修改</el-button>
              <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['system:dict:remove']" >删除</el-button>
              </span>
            </el-table-column>
          </template>
        </cfTable>
        <pagination :total='queryParams.total' :page.sync='queryParams.pageNum' :limit.sync='queryParams.pageSize' @pagination='getListFn'/>
      </template>
    </cfCard>
    <updateDictData v-if="dialogObj.visible" :dialogObj="dialogObj" @refresh="getListFn"></updateDictData>
  </div>
</template>

<script>
import { listData, exportData, delData } from "@/api/system/dict/data";
import { optionselect as getDictOptionselect, getType } from "@/api/system/dict/type";
import updateDictData from "./updateDictData.vue"
export default {
  props: ['pageDialogObj'],
  name: "Data",
  components: {updateDictData},
  data() {
    return {
      loading: true, /**遮罩层*/ 
      showSearch: true, /**显示搜索条件*/ 
      total: 0,
      tableData: [], /**字典表格数据*/ 
      defaultDictType: "",/**默认字典类型*/ 
      dialogObj: {
        visible: false,
        id: '',
        title: '',
        width: '538px',
        dictType: '',
        appendToBody: true
      },
      typeOptions: [], /**类型数据字典*/ 
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        total: 0
      },
      searchParams: { /**查询参数*/ 
        dictLabel: undefined,
        dictType: undefined,
        status: undefined
      },
      tableDataObj: {
        id: 'id',
        showSelection: true,
        defaultExpandAll: true,
        treeProps: {children: 'children', hasChildren: 'hasChildren'},
        option: [
          {id: 1, label: '字典编码', prop: 'dictCode', align: 'center', width: 100, show: 1, needSlot: false},
          {id: 2, label: '字典标签', prop: 'dictLabel', show: 1, needSlot: true},
          {id: 3, label: '字典键值', prop: 'dictValue', align: 'center', show: 1, needSlot: false},
          {id: 4, label: '字典排序', prop: 'dictSort', align: 'center', show: 1, needSlot: false},
          {id: 5, label: '状态', prop: 'status', show: 1, needSlot: true},
          {id: 6, label: '备注', prop: 'remark', show: 1, needSlot: false},
          {id: 7, label: '创建时间', prop: 'createTime', show: 1, needSlot: true},
        ]
      },
    };
  },
  async created() {
    const dictId = this.pageDialogObj.id;
    await this.getType(dictId);
    await this.getSysNormalDisable();
    this.getTypeList();
  },
  methods: {
    async getType(dictId) {  /** 查询字典类型详细 */
      const res = await getType(dictId);
      if(res.code == 200) {
        this.queryParams.dictType = res.data.dictType;
        this.defaultDictType = res.data.dictType;
        this.getListFn();
      }
    },
    async getTypeList() { /** 查询字典类型列表 */
      const res = await getDictOptionselect();
      if(res.code == 200) {
        this.typeOptions = res.data;
      }
    },
    async getListFn() { /** 查询字典数据列表 */
      this.loading = true;
      const data = {
        ...this.searchParams,
        ...this.queryParams
      }
      const res = await listData(data);
      if(res.code == 200) {
        this.tableData = res.rows;
        this.queryParams.total = res.total;
      }
      setTimeout(() => {
        this.loading = false;
      }, 500);
    },
    handleSearch() {  /** 搜索按钮操作 */
      this.queryParams.pageNum = 1;
      this.queryParams.total = 0;
      this.tableData = [];
      this.getListFn();
    },
    handleReset() {  /** 重置按钮操作 */
      this.resetForm("searchParams", ()=>{
        this.queryParams.dictType = this.defaultDictType;
        this.handleSearch();
      });
    },
    handleAdd() {  /** 新增按钮操作 */
      this.dialogObj.visible = true;
      this.dialogObj.title = "添加字典数据";
      this.dialogObj.id = "";
      this.dialogObj.dictType = this.queryParams.dictType;
    },
    handleUpdate(row) {  /** 修改按钮操作 */
      this.dialogObj.visible = true;
      this.dialogObj.title = "添加字典数据";
      this.dialogObj.id = row.dictCode;
      this.dialogObj.dictType = this.queryParams.dictType;
    },
    handleDelete(row) {  /** 删除按钮操作 */
      const dictCodes = row.dictCode;
      this.$modal.confirm('是否确认删除字典编码为"' + dictCodes + '"的数据项？').then(async()=> {
        this.loading = true;
        const res = await delData(dictCodes);
        if(res.code == 200) {
          this.handleSearch();
          this.$modal.msgSuccess("删除成功");
          this.$store.dispatch('dict/removeDict', this.queryParams.dictType);
        }
        setTimeout(() => {
          this.loading = false;
        }, 500);
      }).catch(() => {});
    },
    async handleExport() {  /** 导出按钮操作 */
      this.loading = true;
      let res = await exportData(this.queryParams);
      if(res.data && res.status == 200) {
        this.downloadFilesFn(res, `data_${this.formatDate()}.xlsx`);
      }
      setTimeout(() => {
        this.loading = false;
      }, 500);
    }
  }
};
</script>